* Specify computer
local comp="byrned"
*local comp="davidbyrne10"

* Specify input and output directories
local datdir = "/Users/`comp'/Dropbox/Research/Billcap/Stata/Data/"
local figdir = "/Users/`comp'/Desktop/ReStatFigs/"
local paperdir= "/Users/`comp'/Dropbox/Research/Billcap/Writing_presenting/TellmeSomething/"

* IPWs
qui gen ipw=.
replace ipw=1 if read_month<=632
forval t=633(1)641){
	qui logit T preT_dailykWh use_above use_corr use_below Nqtr_start1-Nqtr_start6 `demogvars' if read_month==`t'
	qui predict ptmp if read_month==`t' 
	qui replace ipw=(T==1)*(1/ptmp)+(T==0)*(1/(1-ptmp)) if read_month==`t'
	drop ptmp
}

* Checking
*qui logit T preT_dailykWh use_above use_corr use_below Nqtr_start1-Nqtr_start6 `demogvars'  
*qui predict ptmp 
*qui replace ipw=(T==1)*(1/ptmp)+(T==0)*(1/(1-ptmp)) 
*drop ptmp

* Note: for Table A.5 remove "[pweight=ipw]" in all of the regressions below

* Scale covariates, Demographic Variables
foreach var in T T__no_ans T__use_above T__use_corr T__use_below T__use_q1 T__use_q2 T__use_q4 T__use_q5 T__median_HHincome T__av_age T__PFullTime T__PRented T__green{
	qui replace `var'=`var'/100
}
foreach var in A A__no_ans A__use_above A__use_corr A__use_below A__use_q1 A__use_q2 A__use_q4 A__use_q5 A__median_HHincome A__av_age A__PFullTime A__PRented A__green{
	qui replace `var'=`var'/100
}
qui sum median_HHincome, detail
qui gen inc=cond(median_HHincome>r(p50),1,0)
qui sum av_age, detail
qui gen age=cond(av_age>r(p50),1,0)		
qui sum PFullTime, detail
qui gen emp=cond(PFullTime>r(p50),1,0)
qui sum PRented, detail
qui gen rent=cond(PRented>r(p50),1,0)		
qui sum PRented, detail	
qui gen env=cond(green>r(p50),1,0)

foreach var1 in T A postT1 postT2{
	foreach var2 in inc age emp rent env{
		qui gen `var1'__`var2'=`var1'*`var2'
	}
}

* Pooled Heterogeneous Treatment Effects Regressions
local varlist1="T"
qui areg ldailykWh `varlist1' read_month2-read_month11 [pweight=ipw], absorb(account_number) cluster(account_number)
eststo m11
qui matrix temp=e(b)'
qui svmat temp, names(est1)
qui replace est1=. if _n>=2

local varlist1="T T__use_above T__use_corr T__use_below postT1__use_above postT1__use_corr postT1__use_below postT2__use_above postT2__use_corr postT2__use_below"
qui areg ldailykWh `varlist1' read_month2-read_month11 [pweight=ipw], absorb(account_number) cluster(account_number)
eststo m12	
qui matrix temp=e(b)'
qui svmat temp, names(est2)
qui replace est2=. if _n>=5

local varlist1="T T__use_above T__use_corr T__use_below postT1__use_above postT1__use_corr postT1__use_below postT2__use_above postT2__use_corr postT2__use_below T__use_q1 T__use_q2 T__use_q4 T__use_q5 postT1__use_q1 postT1__use_q2 postT1__use_q4 postT1__use_q5 postT2__use_q1 postT2__use_q2 postT2__use_q4 postT2__use_q5"									
qui areg ldailykWh `varlist1' read_month2-read_month11 [pweight=ipw], absorb(account_number) cluster(account_number)
eststo m13	
qui matrix temp=e(b)'
qui svmat temp, names(est3)
qui replace est3=. if (_n>=5 & _n<=10) | _n>=15
qui replace est3=est3[_n+6] if _n>=5

local varlist1="T T__use_above T__use_corr T__use_below postT1__use_above postT1__use_corr postT1__use_below postT2__use_above postT2__use_corr postT2__use_below T__inc T__age T__emp T__rent T__env postT1__inc postT1__age postT1__emp postT1__rent postT1__env postT2__inc postT2__age postT2__emp postT2__rent postT2__env"							
qui areg ldailykWh `varlist1' read_month2-read_month11 [pweight=ipw], absorb(account_number) cluster(account_number)
eststo m14
qui matrix temp=e(b)'
qui svmat temp, names(est4)
qui replace est4=. if (_n>=5 & _n<=10) | _n>=16
qui replace est4=est4[_n+2] if _n>=9

local varlist1="T T__use_above T__use_corr T__use_below postT1__use_above postT1__use_corr postT1__use_below postT2__use_above postT2__use_corr postT2__use_below T__use_q1 T__use_q2 T__use_q4 T__use_q5 postT1__use_q1 postT1__use_q2 postT1__use_q4 postT1__use_q5 postT2__use_q1 postT2__use_q2 postT2__use_q4 postT2__use_q5 T__inc T__age T__emp T__rent T__env postT1__inc postT1__age postT1__emp postT1__rent postT1__env postT2__inc postT2__age postT2__emp postT2__rent postT2__env"				
qui areg ldailykWh `varlist1' read_month2-read_month11 [pweight=ipw], absorb(account_number) cluster(account_number)
eststo m15	
matrix temp=e(b)'
svmat temp, names(est5)
qui replace est5=. if (_n>=5 & _n<=10) | (_n>=15 & _n<=22) | _n>=28
qui replace est5=est5[_n+6] if (_n>=5 & _n<=8)
qui replace est5=est5[_n+14] if _n>=9
	
local varlist1="A A__use_above A__use_corr A__use_below A__use_q1 A__use_q2 A__use_q4 A__use_q5"			
local varlist2="postT1__use_above postT1__use_corr postT1__use_below postT2__use_above postT2__use_corr postT2__use_below postT1__use_q1 postT1__use_q2 postT1__use_q4 postT1__use_q5 postT2__use_q1 postT2__use_q2 postT2__use_q4 postT2__use_q5"
local ivlist="T T__use_above T__use_corr T__use_below T__use_q1 T__use_q2 T__use_q4 T__use_q5"
qui xtivreg2 ldailykWh `varlist2' (`varlist1'=`ivlist') read_month2-read_month11 [pweight=ipw], fe cluster(account_number)
eststo m23	
qui matrix temp=e(b)'
qui svmat temp, names(est6)	
qui replace est6=. if _n>=9
	
local varlist1="A A__use_above A__use_corr A__use_below A__use_q1 A__use_q2 A__use_q4 A__use_q5 A__inc A__age A__emp A__rent A__env"
local varlist2="postT1__use_above postT1__use_corr postT1__use_below postT2__use_above postT2__use_corr postT2__use_below postT1__use_q1 postT1__use_q2 postT1__use_q4 postT1__use_q5 postT2__use_q1 postT2__use_q2 postT2__use_q4 postT2__use_q5 postT1__inc postT1__age postT1__emp postT1__rent postT1__env postT2__inc postT2__age postT2__emp postT2__rent postT2__env"
local ivlist="T T__use_above T__use_corr T__use_below T__use_q1 T__use_q2 T__use_q4 T__use_q5 T__inc T__age T__emp T__rent T__env"	
qui xtivreg2 ldailykWh `varlist2' (`varlist1'=`ivlist') read_month2-read_month11 [pweight=ipw], fe cluster(account_number)
eststo m25
qui matrix temp=e(b)'
qui svmat temp, names(est7)
qui replace est7=. if _n>=14	


		
keep est1 est2 est3 est4 est5 est6 est7	
drop if est7==.	
gen param=_n
